// pages/user/user.js
var app = getApp();

const JMessage = require('../../utils/jmessage-wxapplet-sdk-1.4.0.min.js');
const utilMd5 = require('../../utils/md5.js');
const fetch = require('../../utils/fetch');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    doctorInfo: {},
    tempFilePaths: {},
    textareaMsg: ""
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    this.setData({
      doctorInfo: app.doctorInfo
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  //上传
  // upload: function () {
  //   let that = this;
  //   wx.chooseImage({
  //     count: 3, // 默认9
  //     sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
  //     sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
  //     success: function (res) {
  //       wx.showToast({
  //         title: '正在上传...',
  //         icon: 'loading',
  //         mask: true,
  //         duration: 1000
  //       })
  //       var tempFilePaths = res.tempFilePaths
  //       that.setData({
  //         tempFilePaths: tempFilePaths
  //       })
  //     }
  //   })
  // },
  takeMsg: function (e) {
    this.setData({
      textareaMsg: e.detail.value
    })
  },

  submitMsg: function () {
    wx.showLoading({
      title: '加载中',
      mask: true
    });
    var JIM = new JMessage();
    var myDate = new Date().getTime();
    var signature = utilMd5.hexMD5("appkey=00ca9e9d16207e781db439b4&timestamp=" + myDate + "&random_str=abcdefghijk_123456789&key=83e09cf66db6e3b1f69366d3");
    const that = this;
    //极光初始化
    JIM.init({
      "appkey": "00ca9e9d16207e781db439b4",
      "random_str": "abcdefghijk_123456789",
      "signature": signature,
      "timestamp": myDate
    }).onSuccess(function (data) {
      //极光登录
      JIM.login({
        'username': app.userIMinfo.imUsername,
        'password': app.userIMinfo.imPassword,
        'is_md5': true
      }).onSuccess(function (data) {
        JIM.sendSingleMsg({
          'target_username': app.doctorInfo.imUsername,
          'content': that.data.textareaMsg
        }).onSuccess(function (data, msg) {

          const firstData = {};
          firstData.userImName = app.userIMinfo.imUsername;
          firstData.doctorImName = app.doctorInfo.imUsername;

          fetch("/inquiryDoctor/Messages/sendTime", firstData, "POST").then(res => {
            console.log(res)
          });


          wx.hideLoading();
          wx.showToast({
            title: '发送成功',
            icon: 'success',
            duration: 1500
          })
          setTimeout(() => {
            wx.reLaunch({
              url: '../record/record?id=' + app.doctorInfo.imUsername + '&msg=' + that.data.textareaMsg + '&time=' + myDate
            })
          }, 2000);
        }).onFail(function (data) {
          wx.showToast({
            title: '发送失败',
            icon: 'fail',
            duration: 1500
          })
        });

      }).onFail(function (data) {
        console.log(data)
        wx.showToast({
          title: '请求超时',
          icon: 'fail',
          duration: 1500
        })
      })
    }).onFail(function (data) {
      console.log(data)
      wx.showToast({
        title: '请求超时',
        icon: 'fail',
        duration: 1500
      })
    });
  }

})